/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import who.tmap.bo.ThematicMap;

/**
 * This dialog is dedicated to thematic map properties
 * For the moment, it is only dealing with linked files for .tml 
 * @author stl
 *
 */
public class JDialogThematicMapProperties extends JDialog {

	private JTabbedPane jTabbedPane;
	private JPanelGlobalPreferences jPanelGlobalPreferences;
	private JPanelDigitizer jPanelDigitizer;
	private JPanel mainContainer;
	
	private Box buttonBox;
	private JButton jButtonSave;
	private JButton jButtonCancel;
	
	private ThematicMap thematicMap;
	
	private boolean saveButtonPressed =false;
	
	public JDialogThematicMapProperties( Frame parent, boolean modal, ThematicMap thematicMap ) {
		super( parent, modal );
		this.thematicMap =thematicMap;
		createAndShowGUI();
	}
	
	private void createAndShowGUI() {
		jTabbedPane =new JTabbedPane();
		jPanelGlobalPreferences =new JPanelGlobalPreferences(thematicMap);
		jPanelDigitizer =new JPanelDigitizer();
		mainContainer =new JPanel( new GridBagLayout() );
		GridBagConstraints cc =new GridBagConstraints();
				
		jTabbedPane.add( "Thematic Map Properties", jPanelGlobalPreferences );
		jTabbedPane.add( "Digitizer", jPanelDigitizer );
		
		cc.gridx =0;
		cc.gridy =0;
		cc.gridheight =1;
		cc.gridwidth =1;
		cc.weightx =1;
		cc.weighty =1;
		cc.insets =new Insets(5,5,5,5);
		cc.anchor =GridBagConstraints.NORTHWEST;
		cc.fill =GridBagConstraints.HORIZONTAL;
		mainContainer.add( jTabbedPane,cc );
		
		cc.gridx =0;
		cc.gridy =0;
		cc.gridheight =1;
		cc.gridwidth =1;
		cc.weightx =1;
		cc.weighty =1;
		cc.insets =new Insets(5,5,5,5);
		cc.anchor =GridBagConstraints.NORTHWEST;
		cc.fill =GridBagConstraints.BOTH;
		mainContainer.add( jTabbedPane,cc );
		
		cc.gridx =0;
		cc.gridy =1;
		cc.gridheight =1;
		cc.gridwidth =1;
		cc.weightx =0;
		cc.weighty =0;
		cc.insets =new Insets(5,5,5,5);
		cc.anchor =GridBagConstraints.SOUTHEAST;
		cc.fill =GridBagConstraints.NONE;
		mainContainer.add( getButtonBox(),cc );
		
		setMinimumSize( new Dimension( 600, 550) );
		setPreferredSize( new Dimension( 600,550) );
		setSize( getPreferredSize() );
		
		setContentPane( mainContainer );
		setTitle( "Preferences" );
		
		setLocationRelativeTo( getParent() );	
	}

	public JPanelGlobalPreferences getJPanelGlobalPreferences() {
		return jPanelGlobalPreferences;
	}

	public JPanelDigitizer getJPanelDigitizer() {
		return jPanelDigitizer;
	}
	
	private JButton getJButtonSave() {
		if ( jButtonSave == null ){
			jButtonSave =new JButton("Save");
			jButtonSave.addActionListener( new SaveAction() );
			jButtonSave.setPreferredSize( new Dimension( 90 , 30) );
			jButtonSave.setMaximumSize( new Dimension( 90 , 30)  );
			jButtonSave.setMinimumSize( new Dimension( 90 , 30) );
		}
		return jButtonSave;
	}
	
	private JButton getJButtonCancel() {
		if( jButtonCancel == null ){
			jButtonCancel =new JButton("Cancel");
			jButtonCancel.addActionListener( new CancelAction() );
			jButtonCancel.setPreferredSize( new Dimension( 90 , 30) );
			jButtonCancel.setMaximumSize( new Dimension( 90 , 30)  );
			jButtonCancel.setMinimumSize( new Dimension( 90 , 30) );
		}
		return jButtonCancel;
	}
	
	private Box getButtonBox() {
		if( buttonBox == null ){
			buttonBox =Box.createHorizontalBox();
			buttonBox.add( getJButtonSave() );
			buttonBox.add( Box.createHorizontalStrut( 7 ));
			buttonBox.add( getJButtonCancel() );
		}
		return buttonBox;
	}
	
	private class SaveAction extends AbstractAction {
		
		public void actionPerformed(ActionEvent arg0) {
			if ( jPanelDigitizer.save() ) {
				jPanelGlobalPreferences.save();
				saveButtonPressed =true;
				setVisible( false );
			}
		}
		
	}
	
	private class CancelAction extends AbstractAction {
		
		public void actionPerformed(ActionEvent e) {
			dispose();					
		}		
		
	}
	
	public boolean isSaveButtonPressed() {
		return saveButtonPressed;
	}
}
